# coding=utf-8

import sys
from msvcrt import getch
from random import shuffle

NUM_MAP = {
    '1' : '大树',
    '2' : '鸭子',
    '3' : '耳朵',
    '4' : '红旗',
    '5' : '手掌，钩子',
    '6' : '勺子',
    '7' : '锄头',
    '8' : '眼镜',
    '9' : '球拍',
    '10' : '棒球',
    '11' : '筷子',
    '12' : '婴儿',
    '13' : '医生',
    '14' : '钥匙',
    '15' : '鹦鹉',
    '16' : '石榴',
    '17' : '仪器',
    '18' : '摇把',
    '19' : '衣架',
    '20' : '耳环',
    '21' : '鳄鱼',
    '22' : '鸳鸯',
    '23' : '和尚，乔丹',
    '24' : '粮食',
    '25' : '二胡',
    '26' : '二流子',
    '27' : '耳机',
    '28' : '恶霸',
    '29' : '阿胶',
    '30' : '森林，三菱',
    '31' : '鲨鱼',
    '32' : '扇儿',
    '33' : '蝴蝶',
    '34' : '绅士',
    '35' : '珊瑚',
    '36' : '三鹿（奶粉）',
    '37' : '山鸡',
    '38' : '沙发',
    '39' : '三九（胃药）',
    '40' : '司令',
    '41' : '石英',
    '42' : '柿儿',
    '43' : '石山',
    '44' : '石狮',
    '45' : '师傅',
    '46' : '丝路',
    '47' : '司机',
    '48' : '鼠标',
    '49' : '世界（地球仪）',
    '50' : '涡轮',
    '51' : '乌鸦',
    '52' : '木耳',
    '53' : '武松，武僧',
    '54' : '卫士',
    '55' : '火车',
    '56' : '蜗牛',
    '57' : '武器',
    '58' : '苦瓜',
    '59' : '五角（星）',
    '60' : '榴莲',
    '61' : '六一，儿童',
    '62' : '驴儿',
    '63' : '硫酸',
    '64' : '螺丝',
    '65' : '螺母',
    '66' : '溜溜球',
    '67' : '油漆',
    '68' : '喇叭',
    '69' : '辣椒',
    '70' : '麒麟',
    '71' : '奇异果，蜥蜴',
    '72' : '企鹅',
    '73' : '泉水',
    '74' : '骑士',
    '75' : '青蛙',
    '76' : '气流',
    '77' : '铅球',
    '78' : '钱包',
    '79' : '气球，全家',
    '80' : '巴黎，保龄球',
    '81' : '白蚁',
    '82' : '板儿',
    '83' : '花生',
    '84' : '巴士',
    '85' : '白虎',
    '86' : '八路（军）',
    '87' : '白旗',
    '88' : '爸爸，斑马',
    '89' : '白酒',
    '90' : '精灵',
    '91' : '球衣',
    '92' : '球儿',
    '93' : '球生（圈）',
    '94' : '礁石',
    '95' : '酒壶',
    '96' : '酒楼',
    '97' : '香港',
    '98' : '酒吧',
    '99' : '舅舅，胶卷',
    '00' : '铃铛',
    '01' : '鲤鱼',
    '02' : '冻鹅',
    '03' : '冻虾',
    '04' : '零食',
    '05' : '莲藕',
    '06' : '冻牛',
    '07' : '冷气',
    '08' : '篱笆',
    '09' : '灵柩',
}

def IntToKey(n):
    KEY_COUNT = len(NUM_MAP)
    n = (n - 1) % KEY_COUNT + 1
    return str(n)[-2:]

def KeyToAnswer(key):
    val = NUM_MAP[key]
    print("%s => " % key, end="")
    sys.stdout.flush()
    getch()
    print(val)

def AnswerToKey(key):
    val = NUM_MAP[key]
    print("%s <= " % val, end="")
    sys.stdout.flush()
    getch()
    print(key)

def SequenceTest(Start, N, f):
    for i in range(Start, Start+N):
        key = IntToKey(i)
        f(key)

def RandomTest(n, f):
    keys = [x+1 for x in range(len(NUM_MAP))]
    shuffle(keys)
    for i in range(n):
        key = IntToKey(keys[i])
        f(key)

def RunSequenceTest():
    start = input("please input start => ")
    num = input("please input count => ")
    mode = input("please input mode (1 - key-to-val; 2 - val-to-key) => ")
    SequenceTest(int(start), int(num), KeyToAnswer if mode == "1" else AnswerToKey)

def RunRandomTest():
    num = input("please input count => ")
    mode = input("please input mode (1 - key-to-val; 2 - val-to-key) => ")
    RandomTest(int(num), KeyToAnswer if mode == "1" else AnswerToKey)

def Menu():
    while True:
        print("s - sequence, r - random, q - quit")
        k = getch().decode()
        if k not in ('s', 'r', 'q'):
            continue
        if k == 'q':
            print("quit ...")
            break
        elif k == 's':
            RunSequenceTest()
        elif k == 'r':
            RunRandomTest()

if __name__ == "__main__":
    Menu()
